package com.sj.purchase.modules.cost.config.task.synchronizedata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sj.purchase.modules.cost.repository.order.StockAdjustRepository;
import com.sj.purchase.modules.cost.domain.WarehouseEnum;
import com.sj.purchase.modules.cost.domain.order.AdjustOrder;
import com.sj.purchase.util.DateUtils;
import com.sj.purchase.util.StockUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yangrd on 2017/10/25
 **/
@Component
class AdjustOrderTask2 {

    private static final Logger logger = LoggerFactory.getLogger(AdjustOrderTask2.class);

    @Autowired
    private RestTemplate restTemplate;

    @Value("${sj.ecwms}")
    private String ecwmsApi;

    @Autowired
    private StockAdjustRepository adjustRepository;

    private static String FORMAT = "yyyy/MM/dd";

    /**
     * @param day yyyy/MM/dd
     */
    @Transactional
    public void synchronizeAdjustOrder(String day) {
        Date tomorrow = null;
        try {
            tomorrow = DateUtils.addDays(DateUtils.parseDate(day, FORMAT), 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String tomorrowStr = DateFormatUtils.format(tomorrow, FORMAT);
        String url = String.format("%s/adjust?start=%s&end=%s", ecwmsApi, day, tomorrowStr);
        JSONArray jsonArray = JSONArray.parseArray(restTemplate.getForObject(url, String.class));
        List<AdjustOrder> adjustOrderList = jsonArray.stream().map(o -> {
            JSONObject jsonObject = (JSONObject) o;

            String adjustmentNo = jsonObject.getString("adjustmentKey");
            String createUser = jsonObject.getString("createUser");
            Date createDate = jsonObject.getDate("createDate");
            WarehouseEnum warehouseEnum = StockUtils.getWarehouse(jsonObject.getString("warehouse"));

            JSONArray adjustmentDetails = jsonObject.getJSONArray("adjustmentDetailList");
            return adjustmentDetails.stream().map(o1 -> {
                JSONObject detail = (JSONObject) o1;
                String id = detail.getString("wmsId");
                String skuNo = detail.getString("sku");
                Integer adjustedQty = detail.getInteger("adjustmentNum");
                return new AdjustOrder(
                        id,
                        adjustmentNo,
                        skuNo,
                        adjustedQty,
                        createUser,
                        createDate,
                        warehouseEnum
                );
            }).collect(Collectors.toList());

        }).flatMap(List::stream).collect(Collectors.toList());

        adjustRepository.save(adjustOrderList);

        System.out.println(url);
        logger.debug("[调整单同步完成]  同步条数 :=> {}  url :=>{}", adjustOrderList.size(), url);
    }

}
